package com.example.wuqujingdianphone.fragment.bookcity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.example.wuqujingdianphone.R;
import com.example.wuqujingdianphone.activity.LoginActivity;
import com.example.wuqujingdianphone.activity.MainActivity;
import com.example.wuqujingdianphone.api.GlobalConfig;
import com.example.wuqujingdianphone.base.BaseFragment;
import com.example.wuqujingdianphone.bean.LoginBean;
import com.example.wuqujingdianphone.utils.MD5Util;
import com.example.wuqujingdianphone.utils.NiceDialogUtils;
import com.example.wuqujingdianphone.utils.SPUtil;
import com.example.wuqujingdianphone.utils.ToastUtil;
import com.example.wuqujingdianphone.view.CommonWebView;
import com.example.wuqujingdianphone.view.X5WebView;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by 29083 on 2018/4/10.
 * <p>
 * 书城--网页展示
 */

public class WebViewFragment extends BaseFragment {
    private static final String TAG = "WebViewFragment";
    @BindView(R.id.fl_content)
    FrameLayout mFlContent;
    /* @BindView(R.id.webView)
     X5WebView mWebView;*/
    private String type;
    private boolean isFirstLoadDate;


    public static WebViewFragment getWebViewFragment(String type) {
        WebViewFragment fragment = new WebViewFragment();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_book_shop_webview, null);
        type = getArguments().getString("type");
        return view;
    }

    @Override
    protected void initData() {
        Log.i(TAG, "initData: ");
        isFirstLoadDate = true;


    }

    @Override
    protected void lazyLoad() {
        Log.i(TAG, "lazyLoad: type=" + type);
        if (isFirstLoadDate) {
            isFirstLoadDate = false;
            initView(type);
        }
    }

    private String App_key = "fed82efeba2963ddb349a351726a33b2";
    private String Identify_key = "3d6c567b5ea821a17df9ecd26a42a98e";
    private int Type_id = 1;
    private String userId;
    private String sign_code;

    @SuppressLint("ClickableViewAccessibility")
    private void initView(String type) {

        final DisplayMetrics dm = new DisplayMetrics();
        WindowManager manager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
        assert manager != null;
        manager.getDefaultDisplay().getMetrics(dm);

        userId = SPUtil.getString(getActivity(), GlobalConfig.ACCOUNT_KEY, "");
        Log.i(TAG, "initView: userId=" + userId);
        //ViseHttp.CONFIG().baseUrl("http://api.183read.cc");

        //启用硬件加速
        try {
            if (Integer.parseInt(android.os.Build.VERSION.SDK) >= 11) {
                getActivity().getWindow().setFlags(
                        android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                        android.view.WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
            }
        } catch (Exception e) {
        }


        if (type != null) {
            String webUrl = "";
            switch (type) {
                case "期刊馆":
                    final CommonWebView mWebView = new CommonWebView(getActivity(), null);
                    //final X5WebView mWebView = new X5WebView(getActivity(), null);
                    mFlContent.addView(mWebView);
                    //webUrl = "http://vertical.183read.cc/wqjd.html";
//                    webUrl = "http://yuedu.183read.cc/organization/wqjd.html?user_id=" + userId;
//                    webUrl = "http://vertical.183read.cc/wqjd.html";
                    webUrl = "http://www.wenqujingdian.com/qikan_frame.html";
                   // mWebView.requestDisallowInterceptTouchEvent(true);
                    mWebView.loadUrl(webUrl);

                    mWebView.setOnKeyListener(new View.OnKeyListener() {
                        @Override
                        public boolean onKey(View v, int keyCode, KeyEvent event) {
                            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                                if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
                                    mWebView.goBack();   //后退
                                    return true;    //已处理     返回true表示被处理否则返回false
                                }
                            }
                            return false;
                        }
                    });
//                    sign_code = MD5Util.getMd5(MD5Util.getMd5(Identify_key) + MD5Util.getMd5(userId));
//                    //Log.i(TAG, "initView: sign_code=" + sign_code);
//
//
//                    String url = "http://api.183read.cc/open_api/rest2.php?act=module.user.session.great&param={" +
//                            "\"identify_key\":" + "\"" + Identify_key + "\"," +
//                            "\"app_key\":" + "\"" + App_key + "\"," +
//                            "\"user_id\":" + "\"" + userId + "\"," +
//                            "\"sign_code\":" + "\"" + sign_code + "\"," +
//                            "\"type_id\":" + "\"" + Type_id + "\"" +
//                            "}";
//
//                   /* String url = "http://api.183read.cc/open_api/rest2.php?act=module.user.session.great&param=";
//                    String strParam = "{" +
//                            "\"identify_key\":" + "\"" + Identify_key + "\"," +
//                            "\"app_key\":" + "\"" + App_key + "\"," +
//                            "\"user_id\":" + "\"" + userId + "\"," +
//                            "\"sign_code\":" + "\"" + sign_code + "\"" +
//                            "}";*/
//
//                    //Log.i(TAG, "initView: url=" + url);
//
//
//                    RequestParams params = new RequestParams(url);
//                    // params.addQueryStringParameter("param", strParam);
//                    x.http().get(params, new Callback.CommonCallback<String>() {
//                        @Override
//                        public void onSuccess(String result) {
//                            Log.i(TAG, "onSuccess: result=" + result);
//                            mWebView.loadUrl("http://yuedu.183read.cc/organization/wqjd.html?user_id=" + userId);
//                        }
//
//                        @Override
//                        public void onError(Throwable ex, boolean isOnCallback) {
//                            Log.i(TAG, "onError: ");
//                        }
//
//                        @Override
//                        public void onCancelled(CancelledException cex) {
//                            Log.i(TAG, "onCancelled: ");
//                        }
//
//                        @Override
//                        public void onFinished() {
//                            Log.i(TAG, "onFinished: ");
//                        }
//                    });
//


                  /*  ViseHttp.GET(url)
                            .tag(TAG)
                            //.addParam("username", strPhone)
                            // .addParam("password", strPassword)
                            .request(new ACallback<String>() {
                                @Override
                                public void onSuccess(String result) {
                                    // NiceDialogUtils.dismissNiceDialog();
                                    Log.i(TAG, "onSuccess: result=" + result);

                                    mWebView.loadUrl("http://yuedu.183read.cc/organization/wqjd.html?user_id=" + userId);
                                }

                                @Override
                                public void onFail(int errCode, String errMsg) {
                                    // NiceDialogUtils.dismissNiceDialog();
                                    //请求失败，errCode为错误码，errMsg为错误描述
                                    ToastUtil.showToast("errCode=" + errCode + " errMsg=" + errMsg);
                                }
                            });*/


                    break;
                case "日习馆":
                    final CommonWebView mWebView2 = new CommonWebView(getActivity(), null);
                    mFlContent.addView(mWebView2);
//                    webUrl = "http://wenqujingdian.com/index.php/Baozhi";
                    webUrl = "http://www.wenqujingdian.com/index.php/Baozhi/index/lm/460.shtml";
                    mWebView2.loadUrl(webUrl);
                    mWebView2.setOnKeyListener(new View.OnKeyListener() {
                        @Override
                        public boolean onKey(View v, int keyCode, KeyEvent event) {
                            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                                if (keyCode == KeyEvent.KEYCODE_BACK && mWebView2.canGoBack()) {
                                    mWebView2.goBack();   //后退
                                    return true;    //已处理     返回true表示被处理否则返回false
                                }
                            }
                            return false;
                        }
                    });
                    break;
                case "书友会":
                    final CommonWebView mWebView3 = new CommonWebView(getActivity(), null);
                    mFlContent.addView(mWebView3);
//                    webUrl = "http://taiyuan.zxhsd.com";
                    webUrl = "http://www.sxtybook.com/";
                    mWebView3.loadUrl(webUrl);
                    mWebView3.setOnKeyListener(new View.OnKeyListener() {
                        @Override
                        public boolean onKey(View v, int keyCode, KeyEvent event) {
                            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                                if (keyCode == KeyEvent.KEYCODE_BACK && mWebView3.canGoBack()) {
                                    mWebView3.goBack();   //后退
                                    return true;    //已处理     返回true表示被处理否则返回false
                                }
                            }
                            return false;
                        }
                    });
                    break;
            }
        }
    }


}
